//
//  HTHomeViewController.m
//  hsdyOrderSystem
//
//  Created by 赖国锋 on 2017/11/8.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HTHomeViewController.h"
#import "HSHomeMuneCell.h"
#import "HSFoodManagedViewController.h"
#import "HSShopDataViewController.h"
#import "HSTableQRViewController.h"
#import "XFMessageTableViewController.h"
#import "HSSettingViewController.h"
#import "HSOpenSecondLevelVC.h"
#import "HSAddQRViewController.h"
#import "WZLBadgeImport.h"
static NSString * cellIdentifier = @"homeMuneCell";

@interface HTHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UIButton *messageB;//消息
@property (weak, nonatomic) IBOutlet UIButton *settingBtn;//设置
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;//头像
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//名称
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;//电话
@property (weak, nonatomic) IBOutlet UITableView *menuTableView;
@property (copy, nonatomic) NSArray *menuNameData;
@property (copy, nonatomic) NSArray *menuImageData;
@property (copy, nonatomic)NSString *iscanopenseconduser;
@end

@implementation HTHomeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self postAllNoread];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

//获取所有未读信息，返回总条数[responseObject[@"content"] intValue]
-(void)postAllNoread{
    WEAKSELF
    [XFPostRequest postgetTotalUnReadByShopId:[BookShareUserInforDefault readUserUid] success:^(id responseObject) {
        LGFLog(@"===%@",responseObject);
        [weakSelf hideLoading];
        if (responseObject == nil)return ;
        if ([responseObject[@"status"] intValue] == 200){
            if ([[responseObject safeJsonObjectForKey:@"content"] integerValue] > 0) {
                 [weakSelf.messageB showBadgeWithStyle:WBadgeStyleRedDot value:[[responseObject safeJsonObjectForKey:@"content"] integerValue] animationType:WBadgeAnimTypeShake];
            }else{
                [weakSelf.messageB showBadgeWithStyle:WBadgeStyleNumber value:nil animationType:WBadgeAnimTypeShake];
            }
        }
    } failure:^(NSError *error) {
        [weakSelf hideLoading];
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:nil];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.tabBarItem.badgeValue = @"1";
//    self.tabBarItem.badgeColor = [UIColor redColor];
    self.view.backgroundColor = LCOLOROFBACK;
    self.iconImageView.layer.cornerRadius =40.0f;
    self.iconImageView.layer.masksToBounds = YES;
     UIImage *iconImage = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString userDataPathWithPath:archiveAvarterImage]];
    if (iconImage!=nil) {
        self.iconImageView.image = iconImage;
    }else{
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[BookShareUserInforDefault readUserAvatarUrl]] placeholderImage:ImageNamed(@"morentouxiang")];
    }

    self.nameLabel.text = [BookShareUserInforDefault readNicknames];
    self.phoneLabel.text = [BookShareUserInforDefault readUserPhone];
    lNOTIF_ADD(changeiconNoti, changeicon);//修改头像
    lNOTIF_ADD(@"changeShopNameNoti", changeNameNoti);//店名
    lNOTIF_ADD(@"changeShopPhoneNoti", changePhoneNoti);//门店电话
   self.iscanopenseconduser = [BookShareUserInforDefault readiscanOpenSecondUser];
//    LGFLog(@"iscanopenseconduser = = %@",self.iscanopenseconduser);
    if ([self.iscanopenseconduser isEqualToString:@"分店长"]) {//可开
        self.menuNameData = @[@"开二级账号",@"门店资料",@"桌位二维码",@"菜品管理"];
        self.menuImageData = @[@"openService",@"shop",@"qrcode",@"caipin"];
    }else{
        self.menuNameData = @[@"门店资料",@"桌位二维码",@"菜品管理"];
        self.menuImageData = @[@"shop",@"qrcode",@"caipin"];
    }
}

-(void)dealloc{
    lNOTIF_REMV();
}

//修改头像
-(void)changeicon{
   UIImage *iconImage = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString userDataPathWithPath:archiveAvarterImage]];
    self.iconImageView.image = iconImage;
}

-(void)changeNameNoti{
     self.nameLabel.text = [BookShareUserInforDefault readNicknames];
}

-(void)changePhoneNoti{
    self.phoneLabel.text = [BookShareUserInforDefault readUserPhone];;
}

-(void)initViews{
    [self.menuTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HSHomeMuneCell class]) bundle:nil] forCellReuseIdentifier:cellIdentifier];
    self.menuTableView.backgroundColor = LCOLOROFBACK;
    self.menuTableView.tableFooterView = [UIView new];
    self.menuTableView.scrollEnabled = NO;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return iPhone5?60:80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.menuNameData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HSHomeMuneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.nameLabel.text = self.menuNameData[indexPath.row];
    cell.nameLabel.textColor = LCOLOROFNAV;
    cell.iconImageView.image = [UIImage imageNamed:self.menuImageData[indexPath.row]];
   
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger row = indexPath.row;
    if ([self.iscanopenseconduser isEqualToString:@"分店长"]) {//可开
        switch (row) {
            case 0://开二级账户
                [self.navigationController pushViewController:[HSOpenSecondLevelVC new] animated:YES];
                break;
            case 1://门店资料
                [self pushToShopDataVC];
                break;
            case 2://桌位二维码
                [self.navigationController pushViewController:[HSAddQRViewController new] animated:YES];
                break;
            case 3://菜品管理
                [self.navigationController pushViewController:[HSFoodManagedViewController new] animated:YES];
                break;
            default:
                break;
        }
    }else{
        switch (row) {
            case 0://门店资料
                [self pushToShopDataVC];
                break;
            case 1://桌位二维码
                [self.navigationController pushViewController:[HSAddQRViewController new] animated:YES];
                break;
            case 2://菜品管理
                [self.navigationController pushViewController:[HSFoodManagedViewController new] animated:YES];
                break;
            default:
                break;
        }
    }
}

-(void)pushToShopDataVC{
    HSShopDataViewController *shopDataVC = [HSShopDataViewController new];
    shopDataVC.iconImage = self.iconImageView.image;
    [self.navigationController pushViewController:shopDataVC animated:YES];
}

#pragma mark - navClick
//消息
-(void)messageBClick{
    lNOTIF_POST(@"clickHomeMessageBtnNoti", nil);
}

//设置
- (IBAction)settingBtnClick:(id)sender {
    HSSettingViewController *settingVC = [HSSettingViewController new];
    [self.navigationController pushViewController:settingVC animated:YES];
}

#pragma mark - lazy
-(NSArray *)menuNameData{
    if (!_menuNameData) {
        _menuNameData = @[].mutableCopy;
    }
    return _menuNameData;
}

-(NSArray *)menuImageData{
    if (!_menuImageData) {
        _menuImageData = @[].mutableCopy;
    }
    return _menuImageData;
}

-(UIButton *)messageB{
    if (!_messageB) {
        _messageB = [UIButton buttonWithType:UIButtonTypeCustom];
        [_messageB setImage:ImageNamed(@"message") forState:0];
        [self.view addSubview:_messageB];
        WEAKSELF
        [_messageB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.view).mas_offset(-56);
            make.top.equalTo(weakSelf.view).mas_offset(32);
            make.width.height.mas_equalTo(22);
        }];
        [_messageB addTarget:self touchUpInsideAction:@selector(messageBClick)];
    }
    return _messageB;
}


@end
